class Area < ActiveRecord::Base
  belongs_to :province, :counter_cache => true
  belongs_to :city, :counter_cache => true
  validates_presence_of :province_id,
    :city_id,
    :name,
    :weather_com_cn_code
  validates_uniqueness_of :weather_com_cn_code
    
  def full_name
    name == '城区' ? city.full_name : (city.full_name + name)
  end
  
  def short_name
    name == '城区' ? city.name : name
  end
  
  def weather_info
    ActiveSupport::JSON.decode(weather_info_json[15,(weather_info_json.size - 16)])    
  end
  
  def today_weather_info
    ActiveSupport::JSON.decode(today_weather_info_json[15,(today_weather_info_json.size - 16)])    
  end

  def self.find_all_by_key_word(key_word)
    all :conditions => [
      'key_words like ?',
      "%#{key_word}%"
    ]
  end

  def send_sm_to(user)
    user.send_sm_by_lite_fetion weather_forecast_sm
  end

  def weather_forecast_sm
     "#{short_name}明天#{weather_info['weather2']},#{weather_info['temp2']},#{weather_info['wind2']}。#{weather_info['index48_d'].present? ? "未来48小时#{weather_info['index48_d']}" : weather_info['index_d']}【麦吐司网】"
  end
  
  def today_weather_sm
    "#{short_name}今天#{weather_info['weather1']},#{weather_info['temp1']},#{weather_info['wind1']},湿度#{today_weather_info['SD']}。#{weather_info['index_d']}}【麦吐司网】"
  end

  def self.capture_weather_infos    
    #all.each_with_object([]) do |area,threads|
    #  threads << Thread.new(area) do |area|
    #    area.capture_weather_info
    #  end
    #end.each do |thread|
    #  thread.join
    #end
    all(:order => 'updated_at asc').each_with_index do |area, index|
      puts "No #{index}. area"
      area.capture_weather_info
      area.capture_today_weather_info
    end
  end  
  
  def capture_weather_info
    begin
      puts "capturing... #{full_name}-#{weather_com_cn_code}'s weather "
      capture_weather_info!
      puts "capture #{full_name}-#{weather_com_cn_code}'s weather success"
    rescue Exception => e
      logger.error "抓取【#{full_name}】天气失败:",e
      puts "capture #{full_name}-#{weather_com_cn_code}'s weather failed : #{e.to_s}"
    end
  end
  
  def capture_weather_info!
    url = URI.parse('http://m.weather.com.cn')
    request = Net::HTTP.start(url.host, url.port) {|http|
      http.get("/data/#{weather_com_cn_code}.html")
    }
    logger.debug "#{full_name} weather's json is : #{request.body}"
    update_attributes :weather_info_json => request.body if request.body.match('city_en')
  end
  
  def capture_today_weather_info
    begin
      puts "capturing... #{full_name}-#{weather_com_cn_code}'s today weather "
      capture_today_weather_info!
      puts "capture #{full_name}-#{weather_com_cn_code}'s today weather success"
    rescue Exception => e
      logger.error "抓取【#{full_name}】今日天气失败:",e
      puts "capture #{full_name}-#{weather_com_cn_code}'s today weather failed : #{e.to_s}"
    end
  end
  
  def capture_today_weather_info!
    url = URI.parse('http://www.weather.com.cn')
    request = Net::HTTP.start(url.host, url.port) {|http|
      http.get("/data/sk/#{weather_com_cn_code}.html")
    }
    logger.debug "#{full_name} weather's json is : #{request.body}"
    update_attributes :today_weather_info_json => request.body if request.body.match('cityid')
  end
end
